package com.bonc.common.sso;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

public class AsiaCipher {
	private static String DEFAULT_CHARSET = "utf-8";
	private static int MD5_BEGIN = 8;
	private static int MD5_END = 24;
	private static int MD5_LEN = 16;
	private static int MD5_INDEX = 0;
	private static int DATA_MINLEN = 16;
	private static int CONTENT_INDEX = 16;

	public static String encrypt(String message, String key) {
		String outputStr = null;
		try {
			String md5Key = HexTransfer.byteArr2HexStr(EncryptMD5
					.encrypt(key)).substring(MD5_BEGIN, MD5_END);
			// 对原文加密
			outputStr = EncryptAES.encrypt(message, md5Key, DEFAULT_CHARSET);
			// 对加密后的密文计算md5码
			String md5Str = HexTransfer.byteArr2HexStr(EncryptMD5
					.encrypt(outputStr));
			// 组装新报文为“32位定长md5+变长密文”
			outputStr = md5Str.substring(MD5_BEGIN, MD5_END) + outputStr;
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			e.printStackTrace();
		} catch (BadPaddingException e) {
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			e.printStackTrace();
		}
		return outputStr;
	}

	public static String dencrypt(String message, String key) {
		String data = null;// 客户端解密后的明文
		if (message != null && message.length() > DATA_MINLEN) {
			String md5Head = message.substring(MD5_INDEX, MD5_LEN);
			// 一次性全部取出密文然后解密
			String content = message.substring(CONTENT_INDEX, message.length());
			String md5Str = null;// 客户端计算的md5
			try {
				// 重新计算md5码
				md5Str = HexTransfer
						.byteArr2HexStr(EncryptMD5.encrypt(content)).substring(
								MD5_BEGIN, MD5_END);
			} catch (NoSuchAlgorithmException e1) {
				e1.printStackTrace();
			}
			if (md5Head.equals(md5Str)) {
				try {
					String md5Key = HexTransfer.byteArr2HexStr(EncryptMD5
							.encrypt(key)).substring(MD5_BEGIN, MD5_END);
					data = EncryptAES.decrypt(content, md5Key,DEFAULT_CHARSET);
				} catch (InvalidKeyException e) {
					e.printStackTrace();
				} catch (IllegalBlockSizeException e) {
					e.printStackTrace();
				} catch (BadPaddingException e) {
					e.printStackTrace();
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				} catch (NoSuchAlgorithmException e) {
					e.printStackTrace();
				} catch (NoSuchPaddingException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();

				}
			}

		}
		return data;

	}

	public static void main(String[] args) {
		try{
			boolean isContinue = true;
			do{
				System.out.println("=================================================");
				System.out.println("1、加密操作");
				System.out.println("2、解密操作");
				System.out.println("3、退出");
				System.out.println("=================================================");
				System.out.println("请输入操作编号：");
				java.util.Scanner sc = new java.util.Scanner(System.in);
				String line = sc.nextLine();
				if ("".equals(line)){
					continue;
				}
				if("3".equals(line.toUpperCase())){
					isContinue = false;
				}else{
					if ("1".equals(line)){
						System.out.println("您选择的是加密操作，请输入待加密数据：");
						String data = sc.nextLine();
						System.out.println("请输入加密密钥：");
						String key = sc.nextLine();
						String result = "";
						try{
							if("".equals(key)){
								result = AsiaEncode.encrypt(data,"utf-8");
							}else{
								result = AsiaCipher.encrypt(data,key);
							}
							System.out.println("加密后的密文是：" + result);
						}catch (Exception ex){
							System.out.println("操作失败！失败原因：" + ex.getMessage());
						}
						System.out.println();
						System.out.println("请按回车键继续....");
						sc.nextLine();
					}else if ("2".equals(line)){
						System.out.println("您选择的是加密操作，请输入待解密数据：");
						String data = sc.nextLine();
						System.out.println("请输入解密密钥：");
						String key = sc.nextLine();
						String result = "";
						try{
							if("".equals(key)){
								result = AsiaEncode.dencrypt(data,"utf-8");
							}else{
								result = AsiaCipher.dencrypt(data,key);
							}

							System.out.println("解密后的明文是：" + result);
						}catch (Exception ex){
							System.out.println("操作失败！失败原因：" + ex.getMessage());
						}
						System.out.println();
						System.out.println("请按回车键继续....");
						sc.nextLine();
					}
				}
			}while (isContinue);

		}catch (Exception ex){
			ex.printStackTrace();
		}
	}
}
